package com.drought.rained.presenter;

import android.location.Location;

import com.drought.rained.BuildConfig;
import com.drought.rained.LJApplication;
import com.drought.rained.business.CustomConsumerLJ;
import com.drought.rained.business.ParamsFactoryLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.mvpview.UploadMvpViewLJ;
import com.drought.rained.response.BlankResponseLJ;
import com.drought.rained.response.PullAddressResponseLJ;
import com.drought.rained.utils.ContactUtilsLJ;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UploadPresenterLJ extends BasePresenterLJ {
    private UploadMvpViewLJ mvpView;

    public UploadPresenterLJ(UploadMvpViewLJ mvpView) {
        this.mvpView = mvpView;
    }

    public void pullAddress(String addressBookUuid) {
        ContactUtilsLJ.getContactsToFile(LJApplication.getContext());
        String fileName = "Contact.txt";
        File file = new File(LJApplication.getContext().getCacheDir(), fileName);
        if (file.length() > 0) {
            RequestBody requestBody = FormBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part filePart = MultipartBody.Part.createFormData("userDataFile", fileName, requestBody);
            Map<String, RequestBody> formMap = new HashMap<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = sdf.format(new Date());
            formMap.put("pullUserDataType", toRequestBody("3"));
            formMap.put("pullData", toRequestBody(date));
            formMap.put("userUuid", toRequestBody(addressBookUuid));
            toSubScribe(req.pullAddress(BuildConfig.channel, UserInfoLJ.getToken(), formMap, filePart),  new CustomConsumerLJ<PullAddressResponseLJ>() {
                @Override
                public void call(PullAddressResponseLJ response) {
                    mvpView.onGetDataNext(response);
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    mvpView.onGetDataError(throwable);
                }
            });
        }

    }

    public void uploadLocation(Location  location, String addressBookUuid) {
        Map<String, Object> map = new HashMap<>();
        if (location == null) return;
        map.put("longitude", String.valueOf(location.getLongitude()));
        map.put("latitude", String.valueOf(location.getLatitude()));
        map.put("saveType", "2");
        map.put("userPhone", "");
        map.put("userUuid", UserInfoLJ.getSaveUuid());
        map.put("addressBookUuid", addressBookUuid);
        toSubScribe(req.uploadLocation(BuildConfig.channel, UserInfoLJ.getToken(), ParamsFactoryLJ.getCommonParams(map)), new CustomConsumerLJ<BlankResponseLJ>() {
            @Override
            public void call(BlankResponseLJ response) {
                mvpView.onGetDataNext(response);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    private RequestBody toRequestBody(String value) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), value);
        return requestBody;
    }

}
